import pytest

from addons import uusing
from utils import compare_values

pytestmark = [pytest.mark.psi, pytest.mark.api]


@uusing("ecpint")
def test_ecp_ghost_sanity():
    import psi4

    psi4.set_memory("4 GB")
    psi4.geometry(
        """
        units Angstrom
        no_com
        no_reorient
        symmetry c1
        0 1
        Ar      0.000000   0.000000  -1.500000
        Gh(I)   0.000000   0.000000   1.500000
"""
    )

    ### ECP + valence
    psi4.basis_helper(
        """
        assign aug-cc-pvtz
        assign I myecp
        [myecp]
        I     0
        S   11   1.00
            5.546500D+03           1.560000D-04
            8.382140D+02           9.860000D-04
            1.821870D+02           2.792000D-03
            3.121230D+01          -4.325100D-02
            1.953140D+01           2.341340D-01
            8.240990D+00          -7.509430D-01
            2.194550D+00           8.829680D-01
            1.109110D+00           4.620610D-01
            3.746410D-01           2.228600D-02
            1.770800D-01          -4.353000D-03
            8.106100D-02           1.102000D-03
        S   11   1.00
            5.546500D+03          -7.300000D-05
            8.382140D+02          -5.080000D-04
            1.821870D+02          -1.158000D-03
            3.121230D+01           1.219300D-02
            1.953140D+01          -8.785400D-02
            8.240990D+00           3.382000D-01
            2.194550D+00          -5.765500D-01
            1.109110D+00          -4.092980D-01
            3.746410D-01           5.674590D-01
            1.770800D-01           6.124890D-01
            8.106100D-02           1.432310D-01
        S   1   1.00
            3.746410D-01           1.000000D+00
        S   1   1.00
            1.770800D-01           1.000000D+00
        S   1   1.00
            1.001000D-01           1.000000D+00
        S   1   1.00
            4.120000D-02           1.0000000
        P   9   1.00
            1.889880D+02           5.850000D-04
            2.128680D+01           3.692300D-02
            1.003960D+01          -2.353240D-01
            3.451800D+00           3.414830D-01
            1.974560D+00           5.347880D-01
            1.024200D+00           2.651410D-01
            4.494370D-01           2.578700D-02
            1.866480D-01           5.220000D-04
            7.348100D-02           6.060000D-04
        P   9   1.00
            1.889880D+02          -2.560000D-04
            2.128680D+01          -1.168200D-02
            1.003960D+01           8.319200D-02
            3.451800D+00          -1.569700D-01
            1.974560D+00          -2.245180D-01
            1.024200D+00          -1.144510D-01
            4.494370D-01           3.753560D-01
            1.866480D-01           5.751360D-01
            7.348100D-02           2.459170D-01
        P   1   1.00
            5.981000D-01           1.000000D+00
        P   1   1.00
            9.618000D-02           1.000000D+00
        P   1   1.00
            3.590000D-02           1.0000000
        D   9   1.00
            1.326620D+02           5.720000D-04
            3.760540D+01           4.402000D-03
            1.038910D+01          -4.092200D-02
            6.490170D+00           9.966100D-02
            3.454510D+00           3.226630D-01
            1.844130D+00           4.003430D-01
            9.624780D-01           2.683060D-01
            4.728530D-01           8.484700D-02
            1.932000D-01           7.632000D-03
        D   1   1.00
            4.728530D-01           1.000000D+00
        D   1   1.00
            1.932000D-01           1.000000D+00
        D   1   1.00
            7.820000D-02           1.0000000
        F   1   1.00
            4.064000D-01           1.000000D+00
        F   1   1.00
            1.848000D-01           1.0000000
        ****
        I     0
        I-ECP     4     28
        g potential
        1
        2      1.00000000             0.00000000
        s-g potential
        3
        2     40.03337600            49.98964900
        2     17.30057600           281.00655600
        2      8.85172000            61.41673900
        p-g potential
        4
        2     15.72014100            67.41623900
        2     15.20822200           134.80769600
        2      8.29418600            14.56654800
        2      7.75394900            28.96842200
        d-g potential
        4
        2     13.81775100            35.53875600
        2     13.58780500            53.33975900
        2      6.94763000             9.71646600
        2      6.96009900            14.97750000
        f-g potential
        4
        2     18.52295000           -20.17661800
        2     18.25103500           -26.08807700
        2      7.55790100            -0.22043400
        2      7.59740400            -0.22164600
        """,
        key="basis",
        name="ECP+valence",
    )

    psi4.set_options(
        {
            "e_convergence": 1e-8,
            "d_convergence": 1e-8,
        }
    )

    psi4.core.set_output_file("output_ecp_plus_valence.dat", False)

    psi4.energy("scf")
    e1 = psi4.variable("HF TOTAL ENERGY")

    ### valence only
    psi4.basis_helper(
        """
        assign aug-cc-pvtz
        assign I noecp
        [noecp]
        I     0
        S   11   1.00
            5.546500D+03           1.560000D-04
            8.382140D+02           9.860000D-04
            1.821870D+02           2.792000D-03
            3.121230D+01          -4.325100D-02
            1.953140D+01           2.341340D-01
            8.240990D+00          -7.509430D-01
            2.194550D+00           8.829680D-01
            1.109110D+00           4.620610D-01
            3.746410D-01           2.228600D-02
            1.770800D-01          -4.353000D-03
            8.106100D-02           1.102000D-03
        S   11   1.00
            5.546500D+03          -7.300000D-05
            8.382140D+02          -5.080000D-04
            1.821870D+02          -1.158000D-03
            3.121230D+01           1.219300D-02
            1.953140D+01          -8.785400D-02
            8.240990D+00           3.382000D-01
            2.194550D+00          -5.765500D-01
            1.109110D+00          -4.092980D-01
            3.746410D-01           5.674590D-01
            1.770800D-01           6.124890D-01
            8.106100D-02           1.432310D-01
        S   1   1.00
            3.746410D-01           1.000000D+00
        S   1   1.00
            1.770800D-01           1.000000D+00
        S   1   1.00
            1.001000D-01           1.000000D+00
        S   1   1.00
            4.120000D-02           1.0000000
        P   9   1.00
            1.889880D+02           5.850000D-04
            2.128680D+01           3.692300D-02
            1.003960D+01          -2.353240D-01
            3.451800D+00           3.414830D-01
            1.974560D+00           5.347880D-01
            1.024200D+00           2.651410D-01
            4.494370D-01           2.578700D-02
            1.866480D-01           5.220000D-04
            7.348100D-02           6.060000D-04
        P   9   1.00
            1.889880D+02          -2.560000D-04
            2.128680D+01          -1.168200D-02
            1.003960D+01           8.319200D-02
            3.451800D+00          -1.569700D-01
            1.974560D+00          -2.245180D-01
            1.024200D+00          -1.144510D-01
            4.494370D-01           3.753560D-01
            1.866480D-01           5.751360D-01
            7.348100D-02           2.459170D-01
        P   1   1.00
            5.981000D-01           1.000000D+00
        P   1   1.00
            9.618000D-02           1.000000D+00
        P   1   1.00
            3.590000D-02           1.0000000
        D   9   1.00
            1.326620D+02           5.720000D-04
            3.760540D+01           4.402000D-03
            1.038910D+01          -4.092200D-02
            6.490170D+00           9.966100D-02
            3.454510D+00           3.226630D-01
            1.844130D+00           4.003430D-01
            9.624780D-01           2.683060D-01
            4.728530D-01           8.484700D-02
            1.932000D-01           7.632000D-03
        D   1   1.00
            4.728530D-01           1.000000D+00
        D   1   1.00
            1.932000D-01           1.000000D+00
        D   1   1.00
            7.820000D-02           1.0000000
        F   1   1.00
            4.064000D-01           1.000000D+00
        F   1   1.00
            1.848000D-01           1.0000000
      ****
        """,
        key="basis",
        name="only_valence",
    )

    psi4.set_options(
        {
            "e_convergence": 1e-8,
            "d_convergence": 1e-8,
        }
    )

    psi4.core.set_output_file("output_only_valence.dat", False)

    psi4.energy("scf")
    e2 = psi4.variable("HF TOTAL ENERGY")

    ### Compare
    diff = e2 - e1

    assert compare_values(0.0, diff, 7, "ECP + valence vs. only valence")

